/**
 * 
 */
package com.thinkgsp.jxc.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgsp.jxc.persistence.ContactBalanceMapper;
import com.thinkgsp.jxc.persistence.InventoryMapper;
import com.thinkgsp.jxc.persistence.PeriodMapper;
import com.thinkgsp.jxc.persistence.ProductContactMapper;
import com.thinkgsp.jxc.persistence.RpContactMapper;
import com.thinkgsp.jxc.service.PeriodEndForwardService;
import com.thinkgsp.sys.model.SysAccountPeriod;

/**
 * @author evan
 *
 */
@Service("periodEndForwardService")
@Transactional
public class PeriodEndForwardServiceImpl extends GeneralServiceImpl implements
		PeriodEndForwardService {
	@Autowired
	private InventoryMapper invMapper;
	@Autowired
	private ProductContactMapper pcMapper;
	@Autowired
	private ContactBalanceMapper cbMapper;
	@Autowired
	private PeriodMapper periodMapper;
	@Autowired
	private RpContactMapper contactMapper;

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.PeriodEndForward#getNextPeriod()
	 */
	public SysAccountPeriod getNextPeriod() {
		SysAccountPeriod curPeriod = getCurrentPeriod();
		SysAccountPeriod nextPeriod = new SysAccountPeriod();
		nextPeriod.setFyear(curPeriod.getFyear() + (curPeriod.getPeriod()/12));
		nextPeriod.setPeriod((curPeriod.getPeriod() + 1)%12);
		nextPeriod.setCurrent(false);
		return nextPeriod;
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.PeriodEndForward#periodEndForward(com.thinkgsp.sys.model.SysAccountPeriod, com.thinkgsp.sys.model.SysAccountPeriod)
	 */
	public void periodEndForward(SysAccountPeriod curPeriod,
			SysAccountPeriod nextPeriod) {

		long curTime = System.currentTimeMillis();
		log.info("开始期末结转,本期" + curPeriod.getFyear() + "." + curPeriod.getPeriod() + ",下期" + nextPeriod.getFyear() + "." + nextPeriod.getPeriod());
		//结转存货
		invMapper.periodEndForward(curPeriod.getFyear(), curPeriod.getPeriod(), nextPeriod.getFyear(), nextPeriod.getPeriod());
		//结转商品流水
		pcMapper.periodEndForward(curPeriod.getFyear(), curPeriod.getPeriod(), nextPeriod.getFyear(), nextPeriod.getPeriod());
		//结转往来余额
		cbMapper.periodEndForward(curPeriod.getFyear(), curPeriod.getPeriod(), nextPeriod.getFyear(), nextPeriod.getPeriod());
		//结转往来明细表
		contactMapper.periodEndForward(curPeriod.getFyear(), curPeriod.getPeriod(), nextPeriod.getFyear(), nextPeriod.getPeriod());
		//更新当前期间
		periodMapper.updateCurrentPeriod(curPeriod);
		//插入下一期间
		periodMapper.insertNextPeriod(nextPeriod);
		
		log.info("结转完成.用时:" + (System.currentTimeMillis() - curTime)/1000);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.PeriodEndForward#periodEndUnForward(com.thinkgsp.sys.model.SysAccountPeriod)
	 */
	public void periodEndUnForward(SysAccountPeriod curPeriod) {
		
	}

}
